#!/bin/sh
#
#   Parse lighttpd access logs for website hits
#   Chris Dorman, 2020 - CC-BY-SA-NC 3.0
#

# Include configuartion file
. `pwd`/config

# Date string for html generation
datestring=`date +"%Y%m%d-%H%M"`

# Project information
PROJTITLE="SpeedyParse"
PROJVERSION="1.0"

cd $LOGDIR

if [ -f access.log.2.gz ]; then
    echo "Extracting access.log files"
    tar -xzf *.gz
fi

# Parse latest access.log and put within log file in HTML format.

if [ -z "$SECONDARYSEARCH" ]; then
    catchcount=`grep $DOMAIN access.log | grep "${SEARCHSTRING}" | wc -l`
    catchlog=`grep $DOMAIN access.log | grep "${SEARCHSTRING}" | sort`
else
    catchcount=`grep $DOMAIN access.log | grep "${SEARCHSTRING}" | grep "${SECONDARYSEARCH}" | wc -l`
    catchlog=`grep $DOMAIN access.log | grep "${SEARCHSTRING}" | grep "${SECONDARYSEARCH}" | sort`
fi

catchtotal=$catchcount

echo "<!DOCTYPE html>
<html>
<head>
    <title>Lighttpd access logs ~ $date</title>
    <style type='text/css' rel='stylesheet'>
    html { background-color: #454545; color: #bbbbbb; padding: 10px; }
    body { background-color: #313131; padding: 20px; border-radius: 8px; border: solid 1px #222222; margin: 0 auto; }
    h1, h2, h3, h4, h5, h6 { color: #ffffff; padding: 4px; width: 100%; text-align: center; margin: auto; }
    code { white-space: pre-wrap; padding: 5px; color: #00ff00; text-align: left; }
    .footer { width: 100%; text-align: center;
    </style>
</head>
<body>
<h3>Access.log information ~ ${datestring}</h3>
<code>
Searching through lighttpd logs with these parameters: 
&quot;${SEARCHSTRING}&quot;" > $OUTPUTDIR/$DOMAIN-${datestring}.html

if [ ! -z "$SECONDARYSEARCH" ]; then
    echo "&quot;${SECONDARYSEARCH}&quot;" >> $OUTPUTDIR/$DOMAIN-${datestring}.html
fi

echo "
</code>
<b>access.log ~ ${catchcount} hits</b><br />
<code>
${catchlog}
</code>
<br /><br />
" >> $OUTPUTDIR/$DOMAIN-${datestring}.html

# Parse old .log files and output within HTML
for file in access.log.*
do
    if [ -z "$SECONDARYSEARCH" ]; then
        addition=`grep $DOMAIN $file | grep "${SEARCHSTRING}" | wc -l`
        catchlog=`grep $DOMAIN $file | grep "${SEARCHSTRING}" | sort`
    else
        addition=`grep $DOMAIN $file | grep "${SEARCHSTRING}" | grep "${SECONDARYSEARCH}" | wc -l`
        catchlog=`grep $DOMAIN $file | grep "${SEARCHSTRING}" | grep "${SECONDARYSEARCH}" | sort`
    fi

    doaddition=$(( $catchcount + $addition ))

    export catchcount=$doaddition

    echo "<b>$file ~ ${addition} hits</b><br />" >> $OUTPUTDIR/$DOMAIN-${datestring}.html
    echo "<code>${catchlog}</code><br /><br />" >> $OUTPUTDIR/$DOMAIN-${datestring}.html
done

echo "<b>${doaddition} hits total on ${DOMAIN} using above search parameters.</b><br /><br />" >> $OUTPUTDIR/$DOMAIN-${datestring}.html

echo "<div class='footer'>Generated by ${PROJTITLE} ${PROJVERSION}</div></body></html>" >> $OUTPUTDIR/$DOMAIN-${datestring}.html
